<?php

defined('_JEXEC') or die;

jimport('joomla.application.component.modellist');

class ApkipasModelResources extends JModelList
{
	public function __construct($config = array())
	{
		if (empty($config['filter_fields'])) {
			$config['filter_fields'] = array(
					'id', 'id',
					'title', 'title',
					'category', 'category',
					'version', 'version',
					'size', 'size',
					'seller', 'seller',
					'updated','updated'
			);
		}
	
		parent::__construct($config);
	}
	
	protected function populateState($ordering = null, $direction = null)
	{
		// Initialise variables.
		$app = JFactory::getApplication('administrator');
	
		// Load the filter state.
		$search = $this->getUserStateFromRequest($this->context.'.filter.search', 'filter_search');
		$this->setState('filter.search', $search);
	
		// Load the parameters.
		$params = JComponentHelper::getParams('com_apkipas');
		$this->setState('params', $params);
	
		// List state information.
		parent::populateState('updated', 'desc');
	}
	
	protected function getStoreId($id = '')
	{
		// Compile the store id.
		$id.= ':' . $this->getState('filter.search');
		return parent::getStoreId($id);
	}
	
	protected function getListQuery()
	{
		// Create a new query object.
		$db		= $this->getDbo();
		$query	= $db->getQuery(true);
		$user	= JFactory::getUser();
	
		// Select the required fields from the table.
		$query->select('*');
		$query->from('#__apkipas_resources');
	
	
		// Filter by search in title
		$search = $this->getState('filter.search');
		if (!empty($search)) {
			if (stripos($search, 'id:') === 0) {
				$query->where('id = '.(int) substr($search, 3));
			} else {
				$search = $db->Quote('%'.$db->getEscaped($search, true).'%');
				$query->where('title LIKE '.$search);
			}
		}
	
		// Add the list ordering clause.
		$orderCol	= $this->state->get('list.ordering');
		$orderDirn	= $this->state->get('list.direction');
	
		$query->order($db->getEscaped($orderCol.' '.$orderDirn));
		return $query;
	}
	
	public function getItem($pk = null)
	{
		// Initialise variables.
		$pk = JRequest::getVar('id');
		$table = $this->getTable('resources');
	
		if ($pk > 0){
			// Attempt to load the row.
			$return = $table->load($pk);
			// Check for a table object error.
			if ($return === false && $table->getError()){
				$this->setError($table->getError());
				return false;
			}
		}
	
		// Convert to the JObject before adding other data.
		$properties = $table->getProperties(1);
		$item = JArrayHelper::toObject($properties, 'JObject');
	
		if (property_exists($item, 'params'))
		{
			$registry = new JRegistry;
			$registry->loadString($item->params);
			$item->params = $registry->toArray();
		}
	
		return $item;
	}
	
	public function save($data){
		
		// Initialise variables;
		$dispatcher = JDispatcher::getInstance();
		$table = $this->getTable('resources');
		$key = $table->getKeyName();
		$pk = (!empty($data[$key])) ? $data[$key] : (int) $this->getState($this->getName() . '.id');
		$isNew = true;
	
		// Allow an exception to be thrown.
		try
		{
			// Load the row if saving an existing record.
			if ($pk > 0)
			{
				$table->load($pk);
				$isNew = false;
			}
	
			// Bind the data.
			if (!$table->bind($data)){
				$this->setError($table->getError());
				return false;
			}
	
			// Check the data.
			if (!$table->check()){
				$this->setError($table->getError());
				return false;
			}
	
			// Store the data.
			if (!$table->store()){
				$this->setError($table->getError());
				return false;
			}

			// Clean the cache.
			$this->cleanCache();
	
		}
		catch (Exception $e){
			$this->setError($e->getMessage());
			return false;
		}
	
		return $table->id;
	}
}
